#!/bin/bash

# Takes the number of joining nodes as parameters. 
# Default number of joining nodes is 1. 
# Usage example: 
# bash$ bash start_nodes.sh
# bash$ bash start_nodes.sh 4

start_joining_node()
{
  local i=$1;
  echo "Starting joining node number $i..."
  xterm -bg black -fg green -e erl -pa ebin/ -setcookie apa \
        -sname node$i -s node start_join node@$SHORTHOST &
}

if [ -z $BASH ]
then echo "Error: $0: This script must be run by bash (not by sh)"
     echo "Usage example: bash$ bash start_nodes.sh"
     echo "Usage example: bash$ bash start_nodes.sh 4"
     exit 1
fi

JOINS=$1;
if [ -z $JOINS ]
then JOINS=1
fi

SHORTHOST=$(hostname --short)

echo "Going to start a first node, and $JOINS joining nodes"
echo "Short hostname is $SHORTHOST"

echo "Running compiler..."
erl -make
echo "Starting first node..."
xterm -bg black -fg green -e erl -pa ebin/ -setcookie apa -sname node -s node start &

for ((i=1; i <= JOINS; i++))
do start_joining_node $i
done

